package com.nanyang.tms.service.weight.impl;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.flow.invoker.TransOutboundInvoker;
import com.nanyang.tms.api.dto.request.waybill.CalWeightRequest;
import com.nanyang.tms.api.dto.response.waybill.WeightResponse;
import com.nanyang.tms.api.service.weight.CalWeightRpcService;
import com.nanyang.tms.domain.weight.context.WeightContext;
import com.nanyang.tms.service.weight.impl.flow.CalWeightProcessPipelineFactory;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class CalWeightRpcServiceImpl implements CalWeightRpcService{
    @Autowired
	private CalWeightProcessPipelineFactory calWeightProcessPipelineFactory;
	@Override
	public ApiResult<WeightResponse> calWeightDetail(CalWeightRequest calWeightRequest) {
		ApiResult<WeightResponse> apiResult=new ApiResult<WeightResponse>();
		try {
			WeightResponse res=new WeightResponse();
			TransOutboundInvoker invoker = calWeightProcessPipelineFactory.build(calWeightRequest);
			invoker.start();
			WeightContext context = invoker.getContext();
			res.setTotalChargeWeight(context.getCalculateWeightContext().getTotalChargeWeight());
			res.setTotalVolumeWeight(context.getCalculateWeightContext().getTotalVolumeWeight());
			res.setTotalWeight(context.getCalculateWeightContext().getTotalWeight());
			apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
			apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
			apiResult.setData(res);
		} catch (Exception e) {
			log.error("inputParams:{} and errorMessage:{}", calWeightRequest.toString(), e.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
		}
		return apiResult;
	}

}
